package com.wwdx.service.impl;

import com.google.common.collect.Maps;
import com.wwdx.model.WwdxJdorderModel;
import com.wwdx.model.WwdxThemeModel;
import com.wwdx.service.WwdxJdorderServiceI;
import com.wwdx.utils.math.BigDecimalUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 * @date 2018/5/7 18:28
 */
@Service
public class WwdxJdorderServiceImpl extends BaseServiceImpl<WwdxJdorderModel> implements WwdxJdorderServiceI{

    @Override
    public Long selectModelCount(WwdxJdorderModel model){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(4);
        String hql = "select count(1) from WwdxJdorderModel\t"+getParams(model,params);
        return this.count(hql,params);
    }

    private String getParams(WwdxJdorderModel model, Map<String, Object> params){
        StringBuilder sb = new StringBuilder("where 1=1\n");
        return sb.toString();
    }

    @Override
    public List<WwdxJdorderModel> selectModelList(WwdxJdorderModel model, Integer page, Integer rows){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(4);
        String hql = "from WwdxJdorderModel\t"+getParams(model,params)+"\norder by finTime desc";
        return this.excute(find(hql, params, page, rows));
    }

    private List<WwdxJdorderModel> excute(List<WwdxJdorderModel> wwdxJdorderModels){
        wwdxJdorderModels.forEach(wwdxJdorderModel -> {
           //产品名称
           WwdxThemeModel wwdxThemeModel = wwdxThemeService.getByThemeId(Long.valueOf(wwdxJdorderModel.getWareNo()));
           if (wwdxThemeModel != null){
               wwdxJdorderModel.setProductName(wwdxThemeModel.getTitle());
           }
           BigDecimal price = BigDecimalUtils.calculate(wwdxJdorderModel.getCostPrice()).div(100).getBigDecimal();
           wwdxJdorderModel.setPrice(price);
        });
        return wwdxJdorderModels;
    }

    @Override
    public WwdxJdorderModel getByJdOrderId(String orderid) {
        HashMap<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("jdOrderNo",orderid);
        return getByHql("from WwdxJdorderModel where jdOrderNo=:jdOrderNo",params);
    }
}